package cn.edu.ujn.liuklab3.service;

import cn.edu.ujn.liuklab3.entity.Reservation;
import cn.edu.ujn.liuklab3.dao.ReservationDao;
import org.springframework.stereotype.Service;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.PageRequest;

import javax.annotation.Resource;
import java.util.List;

@Service("reservationService")
public class ReservationServiceImpl implements ReservationService {
    @Resource
    private ReservationDao reservationDao;

    @Override
    public Reservation queryById(Integer reserId ) {
        return this.reservationDao.queryById(reserId );
    }

    @Override
    public Page<Reservation> queryByPage(Reservation reservation, PageRequest pageRequest) {
        long total = this.reservationDao.count(reservation);
        return new PageImpl<>(this.reservationDao.queryAllByLimit(reservation, pageRequest), pageRequest, total);
    }

    @Override
    public Reservation insert(Reservation reservation) {
        this.reservationDao.insert(reservation);
        return reservation;
    }

    @Override
    public boolean deleteById(Integer reserId) {
        return this.reservationDao.deleteById(reserId ) > 0;
    }

    @Override
    public cn.edu.ujn.util.Page<Reservation> selectAll(Reservation reservation) {
        List<Reservation> reservations = reservationDao.selectAll(reservation);
        cn.edu.ujn.util.Page<Reservation> page = new cn.edu.ujn.util.Page<>();
        page.setPage(reservation.getStart());
        page.setRows(reservations);
        page.setSize(reservation.getRows());
        page.setTotal(reservationDao.selectCount());
        return page;
    }
}
